
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>用FastDFS一步步搭建文件管理系统(CentOS 7) - Luis Blog</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Luis,"> 
    <meta name="description" content="java开发工程师，喜欢代码，喜欢技术,
一、FastDFS介绍开源地址：https://github.com/happyfish100参考：分布式文件系统FastDFS设计原理参考：FastDFS分布式文件系统

1.简介FastDFS,"> 
    <meta name="author" content="winter chen"> 
    <link rel="alternative" href="atom.xml" title="Luis Blog" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
<link rel="stylesheet" href="/css/diaspora.css">

	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Luis Blog</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://blog.winterchen.com"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">用FastDFS一步步搭建文件管理系统(CentOS 7)</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">用FastDFS一步步搭建文件管理系统(CentOS 7)</h1>
        <div class="stuff">
            <span>四月 10, 2018</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/FastDFS/" rel="tag">FastDFS</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Linux/" rel="tag">Linux</a></li></ul>


        </div>
        <div class="content markdown">
            <p><img src="http://img.winterchen.com/ornella-binni-224979-unsplash.jpg" alt=""></p>
<h1 id="一、FastDFS介绍"><a href="#一、FastDFS介绍" class="headerlink" title="一、FastDFS介绍"></a>一、FastDFS介绍</h1><p>开源地址：<a href="https://github.com/happyfish100" target="_blank" rel="noopener">https://github.com/happyfish100</a><br>参考：<a href="http://blog.chinaunix.net/uid-20196318-id-4058561.html" target="_blank" rel="noopener">分布式文件系统FastDFS设计原理</a><br>参考：<a href="http://www.cnblogs.com/Leo_wl/p/6731647.html" target="_blank" rel="noopener">FastDFS分布式文件系统</a></p>
<a id="more"></a>
<h2 id="1-简介"><a href="#1-简介" class="headerlink" title="1.简介"></a>1.简介</h2><p>FastDFS 是一个开源的高性能分布式文件系统（DFS）。 它的主要功能包括：文件存储，文件同步和文件访问，以及高容量和负载平衡。主要解决了海量数据存储问题，特别适合以中小文件（建议范围：4KB &lt; file_size &lt;500MB）为载体的在线服务。</p>
<p>FastDFS 系统有三个角色：跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。</p>
<p>　　<strong>Tracker Server</strong>：跟踪服务器，主要做调度工作，起到均衡的作用；负责管理所有的 storage server和 group，每个 storage 在启动后会连接 Tracker，告知自己所属 group 等信息，并保持周期性心跳。</p>
<p>　　<strong>Storage Server</strong>：存储服务器，主要提供容量和备份服务；以 group 为单位，每个 group 内可以有多台 storage server，数据互为备份。</p>
<p>　　<strong>Client</strong>：客户端，上传下载数据的服务器，也就是我们自己的项目所部署在的服务器。<br>　　<br>　　<img src="http://img.winterchen.com/WX2s0180410-0ss92113@2x.png" alt="服务基本架构">
　　</p>
<h2 id="2、FastDFS的存储策略"><a href="#2、FastDFS的存储策略" class="headerlink" title="2、FastDFS的存储策略"></a>2、FastDFS的存储策略</h2><p>为了支持大容量，存储节点（服务器）采用了分卷（或分组）的组织方式。存储系统由一个或多个卷组成，卷与卷之间的文件是相互独立的，所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成，一个卷下的存储服务器中的文件都是相同的，卷中的多台存储服务器起到了冗余备份和负载均衡的作用。</p>
<p>在卷中增加服务器时，同步已有的文件由系统自动完成，同步完成后，系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时，可以动态添加卷。只需要增加一台或多台服务器，并将它们配置为一个新的卷，这样就扩大了存储系统的容量。</p>
<h2 id="3、FastDFS的上传过程"><a href="#3、FastDFS的上传过程" class="headerlink" title="3、FastDFS的上传过程"></a>3、FastDFS的上传过程</h2><p>FastDFS向使用者提供基本文件访问接口，比如upload、download、append、delete等，以客户端库的方式提供给用户使用。</p>
<p>Storage Server会定期的向Tracker Server发送自己的存储信息。当Tracker Server Cluster中的Tracker Server不止一个时，各个Tracker之间的关系是对等的，所以客户端上传时可以选择任意一个Tracker。</p>
<p>当Tracker收到客户端上传文件的请求时，会为该文件分配一个可以存储文件的group，当选定了group后就要决定给客户端分配group中的哪一个storage server。当分配好storage server后，客户端向storage发送写文件请求，storage将会为文件分配一个数据存储目录。然后为文件分配一个fileid，最后根据以上的信息生成文件名存储文件。</p>
<p><img src="http://img.winterchen.com/WX20180410-092127@2x.png" alt="FastDFS的上传过程"></p>
<h2 id="4、FastDFS的文件同步"><a href="#4、FastDFS的文件同步" class="headerlink" title="4、FastDFS的文件同步"></a>4、FastDFS的文件同步</h2><p>写文件时，客户端将文件写至group内一个storage server即认为写文件成功，storage server写完文件后，会由后台线程将文件同步至同group内其他的storage server。</p>
<p>每个storage写文件后，同时会写一份binlog，binlog里不包含文件数据，只包含文件名等元信息，这份binlog用于后台同步，storage会记录向group内其他storage同步的进度，以便重启后能接上次的进度继续同步；进度以时间戳的方式进行记录，所以最好能保证集群内所有server的时钟保持同步。</p>
<p>storage的同步进度会作为元数据的一部分汇报到tracker上，tracke在选择读storage的时候会以同步进度作为参考。</p>
<h2 id="5、FastDFS的文件下载"><a href="#5、FastDFS的文件下载" class="headerlink" title="5、FastDFS的文件下载"></a>5、FastDFS的文件下载</h2><p>客户端uploadfile成功后，会拿到一个storage生成的文件名，接下来客户端根据这个文件名即可访问到该文件。<br><img src="http://img.winterchen.com/WX20180410-092140@2x.png" alt="FastDFS的文件下载过程"></p>
<p>跟upload file一样，在downloadfile时客户端可以选择任意tracker server。tracker发送download请求给某个tracker，必须带上文件名信息，tracke从文件名中解析出文件的group、大小、创建时间等信息，然后为该请求选择一个storage用来服务读请求。</p>
<h1 id="二、安装FastDFS环境"><a href="#二、安装FastDFS环境" class="headerlink" title="二、安装FastDFS环境"></a>二、安装FastDFS环境</h1><h2 id="0、前言"><a href="#0、前言" class="headerlink" title="0、前言"></a>0、前言</h2><p>操作环境：CentOS7 X64，以下操作都是单机环境。</p>
<p>我把所有的安装包下载到/softpackages/下，解压到当前目录。</p>
<p>先做一件事，修改hosts，将文件服务器的ip与域名映射(单机TrackerServer环境)，因为后面很多配置里面都需要去配置服务器地址，ip变了，就只需要修改hosts即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># vim &#x2F;etc&#x2F;hosts</span><br><span class="line"></span><br><span class="line">增加如下一行，这是我的IP</span><br><span class="line">118.25.36.41 luischen.cn</span><br><span class="line"></span><br><span class="line">如果要本机访问虚拟机，在C:\Windows\System32\drivers\etc\hosts中同样增加一行</span><br></pre></td></tr></table></figure>

<h2 id="1、下载安装-libfastcommon"><a href="#1、下载安装-libfastcommon" class="headerlink" title="1、下载安装 libfastcommon"></a>1、下载安装 libfastcommon</h2><p>libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库，基础环境，安装即可 。</p>
<h3 id="①-下载libfastcommon"><a href="#①-下载libfastcommon" class="headerlink" title="① 下载libfastcommon"></a>① 下载libfastcommon</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># wget https:&#x2F;&#x2F;github.com&#x2F;happyfish100&#x2F;libfastcommon&#x2F;archive&#x2F;V1.0.7.tar.gz</span><br></pre></td></tr></table></figure>

<h3 id="②-解压"><a href="#②-解压" class="headerlink" title="② 解压"></a>② 解压</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># tar -zxvf V1.0.7.tar.gz</span><br><span class="line"># cd libfastcommon-1.0.7</span><br></pre></td></tr></table></figure>

<h3 id="③-编译、安装"><a href="#③-编译、安装" class="headerlink" title="③ 编译、安装"></a>③ 编译、安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># .&#x2F;make.sh</span><br><span class="line"># .&#x2F;make.sh install</span><br></pre></td></tr></table></figure>

<h3 id="④-libfastcommon-so-安装到了-usr-lib64-libfastcommon-so，但是FastDFS主程序设置的lib目录是-usr-local-lib，所以需要创建软链接。"><a href="#④-libfastcommon-so-安装到了-usr-lib64-libfastcommon-so，但是FastDFS主程序设置的lib目录是-usr-local-lib，所以需要创建软链接。" class="headerlink" title="④ libfastcommon.so 安装到了/usr/lib64/libfastcommon.so，但是FastDFS主程序设置的lib目录是/usr/local/lib，所以需要创建软链接。"></a>④ libfastcommon.so 安装到了/usr/lib64/libfastcommon.so，但是FastDFS主程序设置的lib目录是/usr/local/lib，所以需要创建软链接。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># ln -s &#x2F;usr&#x2F;lib64&#x2F;libfastcommon.so &#x2F;usr&#x2F;local&#x2F;lib&#x2F;libfastcommon.so</span><br><span class="line"># ln -s &#x2F;usr&#x2F;lib64&#x2F;libfastcommon.so &#x2F;usr&#x2F;lib&#x2F;libfastcommon.so</span><br><span class="line"># ln -s &#x2F;usr&#x2F;lib64&#x2F;libfdfsclient.so &#x2F;usr&#x2F;local&#x2F;lib&#x2F;libfdfsclient.so</span><br><span class="line"># ln -s &#x2F;usr&#x2F;lib64&#x2F;libfdfsclient.so &#x2F;usr&#x2F;lib&#x2F;libfdfsclient.so</span><br></pre></td></tr></table></figure>

<h2 id="2、下载安装FastDFS"><a href="#2、下载安装FastDFS" class="headerlink" title="2、下载安装FastDFS"></a>2、下载安装FastDFS</h2><h3 id="①-下载FastDFS"><a href="#①-下载FastDFS" class="headerlink" title="① 下载FastDFS"></a>① 下载FastDFS</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># wget https:&#x2F;&#x2F;github.com&#x2F;happyfish100&#x2F;fastdfs&#x2F;archive&#x2F;V5.05.tar.gz</span><br></pre></td></tr></table></figure>

<h3 id="②-解压-1"><a href="#②-解压-1" class="headerlink" title="② 解压"></a>② 解压</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># tar -zxvf V5.05.tar.gz</span><br><span class="line"># cd fastdfs-5.05</span><br></pre></td></tr></table></figure>

<h3 id="③-编译、安装-1"><a href="#③-编译、安装-1" class="headerlink" title="③ 编译、安装"></a>③ 编译、安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># .&#x2F;make.sh</span><br><span class="line"># .&#x2F;make.sh install</span><br></pre></td></tr></table></figure>

<h3 id="④-默认安装方式安装后的相应文件与目录"><a href="#④-默认安装方式安装后的相应文件与目录" class="headerlink" title="④ 默认安装方式安装后的相应文件与目录"></a>④ 默认安装方式安装后的相应文件与目录</h3><ul>
<li>A、服务脚本：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;fdfs_storaged</span><br><span class="line">&#x2F;etc&#x2F;init.d&#x2F;fdfs_tracker</span><br></pre></td></tr></table></figure>
<ul>
<li>B、配置文件（这三个是作者给的样例配置文件） :</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;fdfs&#x2F;client.conf.sample</span><br><span class="line">&#x2F;etc&#x2F;fdfs&#x2F;storage.conf.sample</span><br><span class="line">&#x2F;etc&#x2F;fdfs&#x2F;tracker.conf.sample</span><br></pre></td></tr></table></figure>

<ul>
<li>C、命令工具在 /usr/bin/ 目录下：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">fdfs_appender_test</span><br><span class="line">fdfs_appender_test1</span><br><span class="line">fdfs_append_file</span><br><span class="line">fdfs_crc32</span><br><span class="line">fdfs_delete_file</span><br><span class="line">fdfs_download_file</span><br><span class="line">fdfs_file_info</span><br><span class="line">fdfs_monitor</span><br><span class="line">fdfs_storaged</span><br><span class="line">fdfs_test</span><br><span class="line">fdfs_test1</span><br><span class="line">fdfs_trackerd</span><br><span class="line">fdfs_upload_appender</span><br><span class="line">fdfs_upload_file</span><br><span class="line">stop.sh</span><br><span class="line">restart.sh</span><br></pre></td></tr></table></figure>

<h3 id="⑤-FastDFS-服务脚本设置的-bin-目录是-usr-local-bin，-但实际命令安装在-usr-bin-下。"><a href="#⑤-FastDFS-服务脚本设置的-bin-目录是-usr-local-bin，-但实际命令安装在-usr-bin-下。" class="headerlink" title="⑤ FastDFS 服务脚本设置的 bin 目录是 /usr/local/bin， 但实际命令安装在 /usr/bin/ 下。"></a>⑤ FastDFS 服务脚本设置的 bin 目录是 /usr/local/bin， 但实际命令安装在 /usr/bin/ 下。</h3><ul>
<li>建立 /usr/bin 到 /usr/local/bin 的软链接</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># ln -s &#x2F;usr&#x2F;bin&#x2F;fdfs_trackerd   &#x2F;usr&#x2F;local&#x2F;bin</span><br><span class="line"># ln -s &#x2F;usr&#x2F;bin&#x2F;fdfs_storaged   &#x2F;usr&#x2F;local&#x2F;bin</span><br><span class="line"># ln -s &#x2F;usr&#x2F;bin&#x2F;stop.sh         &#x2F;usr&#x2F;local&#x2F;bin</span><br><span class="line"># ln -s &#x2F;usr&#x2F;bin&#x2F;restart.sh      &#x2F;usr&#x2F;local&#x2F;bin</span><br></pre></td></tr></table></figure>

<h2 id="3、配置FastDFS跟踪器-Tracker"><a href="#3、配置FastDFS跟踪器-Tracker" class="headerlink" title="3、配置FastDFS跟踪器(Tracker)"></a>3、配置FastDFS跟踪器(Tracker)</h2><p>配置文件详细说明参考：<a href="http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1941456&extra=page%3D1%26filter%3Ddigest%26digest%3D1" target="_blank" rel="noopener">FastDFS 配置文件详解</a></p>
<h3 id="①-进入-etc-fdfs，复制-FastDFS-跟踪器样例配置文件-tracker-conf-sample，并重命名为-tracker-conf。"><a href="#①-进入-etc-fdfs，复制-FastDFS-跟踪器样例配置文件-tracker-conf-sample，并重命名为-tracker-conf。" class="headerlink" title="① 进入 /etc/fdfs，复制 FastDFS 跟踪器样例配置文件 tracker.conf.sample，并重命名为 tracker.conf。"></a>① 进入 /etc/fdfs，复制 FastDFS 跟踪器样例配置文件 tracker.conf.sample，并重命名为 tracker.conf。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;etc&#x2F;fdfs</span><br><span class="line"># cp tracker.conf.sample tracker.conf</span><br><span class="line"># vim tracker.conf</span><br></pre></td></tr></table></figure>

<h3 id="②-编辑tracker-conf-，修改下，其它的默认即可。"><a href="#②-编辑tracker-conf-，修改下，其它的默认即可。" class="headerlink" title="② 编辑tracker.conf ，修改下，其它的默认即可。"></a>② 编辑tracker.conf ，修改下，其它的默认即可。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 配置文件是否不生效，false 为生效</span><br><span class="line">disabled&#x3D;false</span><br><span class="line"></span><br><span class="line"># 提供服务的端口</span><br><span class="line">port&#x3D;22122</span><br><span class="line"></span><br><span class="line"># Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)----------【需要修改】</span><br><span class="line">base_path&#x3D;&#x2F;data&#x2F;fastdfs&#x2F;tracker</span><br><span class="line"></span><br><span class="line"># HTTP 服务端口----------【需要修改】</span><br><span class="line">http.server_port&#x3D;80</span><br></pre></td></tr></table></figure>

<h3 id="③-创建tracker基础数据目录，即base-path对应的目录"><a href="#③-创建tracker基础数据目录，即base-path对应的目录" class="headerlink" title="③ 创建tracker基础数据目录，即base_path对应的目录"></a>③ 创建tracker基础数据目录，即base_path对应的目录</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p &#x2F;data&#x2F;fastdfs&#x2F;tracker</span><br></pre></td></tr></table></figure>

<h3 id="④-防火墙中打开跟踪端口（默认的22122）"><a href="#④-防火墙中打开跟踪端口（默认的22122）" class="headerlink" title="④ 防火墙中打开跟踪端口（默认的22122）"></a>④ 防火墙中打开跟踪端口（默认的22122）</h3><p>centos7的防火墙由firewalld进行管理</p>
<ul>
<li>查看状态：<code>systemctl status firewalld</code></li>
<li>开启：<code>systemctl start firewalld</code></li>
<li>关闭:  <code>systemctl stop firewalld</code></li>
<li>添加开放IP：<code>firewall-cmd --permanent --zone=public --add-port=22122/tcp</code></li>
<li>重启： <code>firewall-cmd --reload</code></li>
</ul>
<p>查看服务的运行状态：<code>netstat -unltp|grep fdfs</code></p>
<h3 id="⑤-启动Tracker"><a href="#⑤-启动Tracker" class="headerlink" title="⑤ 启动Tracker"></a>⑤ 启动Tracker</h3><p>初次成功启动，会在 /data/fastdfs/tracker/ (配置的base_path)下创建 data、logs 两个目录。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">可以用这种方式启动</span><br><span class="line"># &#x2F;etc&#x2F;init.d&#x2F;fdfs_trackerd start</span><br><span class="line"></span><br><span class="line">也可以用这种方式启动，前提是上面创建了软链接，后面都用这种方式</span><br><span class="line"># service fdfs_trackerd start</span><br></pre></td></tr></table></figure>

<p>查看 FastDFS Tracker 是否已成功启动 ，22122端口正在被监听，则算是Tracker服务安装成功</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># netstat -unltp|grep fdfs</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-192214@2x.png" alt=""></p>
<p>关闭Tracker命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># service fdfs_trackerd stop</span><br></pre></td></tr></table></figure>

<h3 id="⑥-设置Tracker开机启动"><a href="#⑥-设置Tracker开机启动" class="headerlink" title="⑥ 设置Tracker开机启动"></a>⑥ 设置Tracker开机启动</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># chkconfig fdfs_trackerd on</span><br><span class="line"></span><br><span class="line">或者：</span><br><span class="line"># vim &#x2F;etc&#x2F;rc.d&#x2F;rc.local</span><br><span class="line">加入配置：</span><br><span class="line">&#x2F;etc&#x2F;init.d&#x2F;fdfs_trackerd start</span><br></pre></td></tr></table></figure>

<h3 id="⑦-tracker-server-目录及文件结构"><a href="#⑦-tracker-server-目录及文件结构" class="headerlink" title="⑦ tracker server 目录及文件结构"></a>⑦ tracker server 目录及文件结构</h3><p>Tracker服务启动成功后，会在base_path下创建data、logs两个目录。目录结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$&#123;base_path&#125;</span><br><span class="line">  |__data</span><br><span class="line">  |   |__storage_groups.dat：存储分组信息</span><br><span class="line">  |   |__storage_servers.dat：存储服务器列表</span><br><span class="line">  |__logs</span><br><span class="line">  |   |__trackerd.log： tracker server 日志文件</span><br></pre></td></tr></table></figure>

<h2 id="4、配置-FastDFS-存储-Storage"><a href="#4、配置-FastDFS-存储-Storage" class="headerlink" title="4、配置 FastDFS 存储 (Storage)"></a>4、配置 FastDFS 存储 (Storage)</h2><h3 id="①-进入-etc-fdfs-目录，复制-FastDFS-存储器样例配置文件-storage-conf-sample，并重命名为-storage-conf"><a href="#①-进入-etc-fdfs-目录，复制-FastDFS-存储器样例配置文件-storage-conf-sample，并重命名为-storage-conf" class="headerlink" title="① 进入 /etc/fdfs 目录，复制 FastDFS 存储器样例配置文件 storage.conf.sample，并重命名为 storage.conf"></a>① 进入 /etc/fdfs 目录，复制 FastDFS 存储器样例配置文件 storage.conf.sample，并重命名为 storage.conf</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;etc&#x2F;fdfs</span><br><span class="line"># cp storage.conf.sample storage.conf</span><br><span class="line"># vim storage.conf</span><br></pre></td></tr></table></figure>

<h3 id="②-编辑storage-conf"><a href="#②-编辑storage-conf" class="headerlink" title="② 编辑storage.conf"></a>② 编辑storage.conf</h3><p>修改以下，其它的默认即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"># 配置文件是否不生效，false 为生效</span><br><span class="line">disabled&#x3D;false </span><br><span class="line"></span><br><span class="line"># 指定此 storage server 所在 组(卷)</span><br><span class="line">group_name&#x3D;group1</span><br><span class="line"></span><br><span class="line"># storage server 服务端口</span><br><span class="line">port&#x3D;23000</span><br><span class="line"></span><br><span class="line"># 心跳间隔时间，单位为秒 (这里是指主动向 tracker server 发送心跳)</span><br><span class="line">heart_beat_interval&#x3D;30</span><br><span class="line"></span><br><span class="line"># Storage 数据和日志目录地址(根目录必须存在，子目录会自动生成)----------【需要修改】</span><br><span class="line">base_path&#x3D;&#x2F;data&#x2F;fastdfs&#x2F;storage</span><br><span class="line"></span><br><span class="line"># 存放文件时 storage server 支持多个路径。这里配置存放文件的基路径数目，通常只配一个目录。</span><br><span class="line">store_path_count&#x3D;1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 逐一配置 store_path_count 个路径，索引号基于 0。</span><br><span class="line"># 如果不配置 store_path0，那它就和 base_path 对应的路径一样。----------【需要修改】</span><br><span class="line">store_path0&#x3D;&#x2F;data&#x2F;fastdfs&#x2F;file</span><br><span class="line"></span><br><span class="line"># FastDFS 存储文件时，采用了两级目录。这里配置存放文件的目录个数。 </span><br><span class="line"># 如果本参数只为 N（如： 256），那么 storage server 在初次运行时，会在 store_path 下自动创建 N * N 个存放文件的子目录。</span><br><span class="line">subdir_count_per_path&#x3D;256</span><br><span class="line"></span><br><span class="line"># tracker_server 的列表 ，会主动连接 tracker_server</span><br><span class="line"># 有多个 tracker server 时，每个 tracker server 写一行----------【需要修改】</span><br><span class="line">tracker_server&#x3D;118.25.36.41:22122</span><br><span class="line"></span><br><span class="line"># 允许系统同步的时间段 (默认是全天) 。一般用于避免高峰同步产生一些问题而设定。</span><br><span class="line">sync_start_time&#x3D;00:00</span><br><span class="line">sync_end_time&#x3D;23:59</span><br><span class="line"># 访问端口----------------------------------------【需要修改】</span><br><span class="line">http.server_port&#x3D;80</span><br></pre></td></tr></table></figure>

<h3 id="③-创建Storage基础数据目录，对应base-path目录"><a href="#③-创建Storage基础数据目录，对应base-path目录" class="headerlink" title="③ 创建Storage基础数据目录，对应base_path目录"></a>③ 创建Storage基础数据目录，对应base_path目录</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># mkdir -p &#x2F;data&#x2F;fastdfs&#x2F;storage</span><br><span class="line"></span><br><span class="line"># 这是配置的store_path0路径</span><br><span class="line"># mkdir -p &#x2F;data&#x2F;fastdfs&#x2F;file</span><br></pre></td></tr></table></figure>

<h3 id="④-防火墙中打开存储器端口（默认的-23000）"><a href="#④-防火墙中打开存储器端口（默认的-23000）" class="headerlink" title="④ 防火墙中打开存储器端口（默认的 23000）"></a>④ 防火墙中打开存储器端口（默认的 23000）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 添加开放IP：</span><br><span class="line">firewall-cmd --permanent --zone&#x3D;public --add-port&#x3D;23000&#x2F;tcp</span><br><span class="line"># 重启： </span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<h3 id="⑤-启动-Storage"><a href="#⑤-启动-Storage" class="headerlink" title="⑤ 启动 Storage"></a>⑤ 启动 Storage</h3><p>启动Storage前确保Tracker是启动的。初次启动成功，会在 /data/fastdfs/storage 目录下创建 data、 logs 两个目录。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">可以用这种方式启动</span><br><span class="line"># &#x2F;etc&#x2F;init.d&#x2F;fdfs_storaged start</span><br><span class="line"></span><br><span class="line">也可以用这种方式，后面都用这种</span><br><span class="line"># service fdfs_storaged start</span><br></pre></td></tr></table></figure>

<p>查看 Storage 是否成功启动，23000 端口正在被监听，就算 Storage 启动成功。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># netstat -unltp|grep fdfs</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-194149@2x.png" alt=""></p>
<p>关闭Storage命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># service fdfs_storaged stop</span><br></pre></td></tr></table></figure>

<p>查看Storage和Tracker是否在通信：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;usr&#x2F;bin&#x2F;fdfs_monitor &#x2F;etc&#x2F;fdfs&#x2F;storage.conf</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-194421@2x.png" alt=""></p>
<h3 id="⑥-设置-Storage-开机启动"><a href="#⑥-设置-Storage-开机启动" class="headerlink" title="⑥ 设置 Storage 开机启动"></a>⑥ 设置 Storage 开机启动</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># chkconfig fdfs_storaged on</span><br><span class="line"></span><br><span class="line">或者：</span><br><span class="line"># vim &#x2F;etc&#x2F;rc.d&#x2F;rc.local</span><br><span class="line">加入配置：</span><br><span class="line">&#x2F;etc&#x2F;init.d&#x2F;fdfs_storaged start</span><br></pre></td></tr></table></figure>

<h3 id="⑦-Storage-目录"><a href="#⑦-Storage-目录" class="headerlink" title="⑦ Storage 目录"></a>⑦ Storage 目录</h3><p>同 Tracker，Storage 启动成功后，在base_path 下创建了data、logs目录，记录着 Storage Server 的信息。</p>
<p>在 store_path0 目录下，创建了N*N个子目录：</p>
<p><img src="http://img.winterchen.com/WX20180409-194639@2x.png" alt=""></p>
<h2 id="5、文件上传测试"><a href="#5、文件上传测试" class="headerlink" title="5、文件上传测试"></a>5、文件上传测试</h2><h3 id="①-修改-Tracker-服务器中的客户端配置文件"><a href="#①-修改-Tracker-服务器中的客户端配置文件" class="headerlink" title="① 修改 Tracker 服务器中的客户端配置文件"></a>① 修改 Tracker 服务器中的客户端配置文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;etc&#x2F;fdfs</span><br><span class="line"># cp client.conf.sample client.conf</span><br><span class="line"># vim client.conf</span><br></pre></td></tr></table></figure>

<p>修改如下配置即可，其它默认。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># Client 的数据和日志目录</span><br><span class="line">base_path&#x3D;&#x2F;data&#x2F;fastdfs&#x2F;client</span><br><span class="line"></span><br><span class="line"># Tracker端口</span><br><span class="line">tracker_server&#x3D;118.25.36.41:22122</span><br></pre></td></tr></table></figure>

<h3 id="②-上传测试"><a href="#②-上传测试" class="headerlink" title="② 上传测试"></a>② 上传测试</h3><p>首先需要创建client目录</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p &#x2F;data&#x2F;fastdfs&#x2F;client</span><br></pre></td></tr></table></figure>

<p> 在linux内部执行如下命令上传 namei.jpeg 图片</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># &#x2F;usr&#x2F;bin&#x2F;fdfs_upload_file &#x2F;etc&#x2F;fdfs&#x2F;client.conf storage.conf</span><br></pre></td></tr></table></figure>

<p> 上传成功后返回文件ID号：</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">group1&#x2F;M00&#x2F;00&#x2F;00&#x2F;Cmlg2FrLU9eAVJ-ZAAAejzOgbzU97.conf</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-195200@2x.png" alt="上传成功"></p>
<p> 返回的文件ID由group、存储目录、两级子目录、fileid、文件后缀名（由客户端指定，主要用于区分文件类型）拼接而成。</p>
<p> <img src="http://img.winterchen.com/1523275193927.jpg" alt=""></p>
<h1 id="三、安装Nginx"><a href="#三、安装Nginx" class="headerlink" title="三、安装Nginx"></a>三、安装Nginx</h1><p>上面将文件上传成功了，但我们无法下载。因此安装Nginx作为服务器以支持Http方式访问文件。同时，后面安装FastDFS的Nginx模块也需要Nginx环境。</p>
<p>Nginx只需要安装到StorageServer所在的服务器即可，用于访问文件。我这里由于是单机，TrackerServer和StorageServer在一台服务器上。</p>
<h2 id="1、安装nginx所需环境"><a href="#1、安装nginx所需环境" class="headerlink" title="1、安装nginx所需环境"></a>1、安装nginx所需环境</h2><h3 id="①-gcc-安装"><a href="#①-gcc-安装" class="headerlink" title="① gcc 安装"></a>① gcc 安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install gcc-c++</span><br></pre></td></tr></table></figure>

<h3 id="②-PCRE-pcre-devel-安装"><a href="#②-PCRE-pcre-devel-安装" class="headerlink" title="② PCRE pcre-devel 安装"></a>② PCRE pcre-devel 安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y pcre pcre-devel</span><br></pre></td></tr></table></figure>

<h3 id="③-zlib-安装"><a href="#③-zlib-安装" class="headerlink" title="③ zlib 安装"></a>③ zlib 安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y zlib zlib-devel</span><br></pre></td></tr></table></figure>

<h3 id="④-OpenSSL-安装"><a href="#④-OpenSSL-安装" class="headerlink" title="④ OpenSSL 安装"></a>④ OpenSSL 安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># yum install -y openssl openssl-devel</span><br></pre></td></tr></table></figure>

<h2 id="2、安装Nginx"><a href="#2、安装Nginx" class="headerlink" title="2、安装Nginx"></a>2、安装Nginx</h2><h3 id="①-下载nginx"><a href="#①-下载nginx" class="headerlink" title="① 下载nginx"></a>① 下载nginx</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># wget -c https:&#x2F;&#x2F;nginx.org&#x2F;download&#x2F;nginx-1.12.1.tar.gz</span><br></pre></td></tr></table></figure>

<h3 id="②-解压-2"><a href="#②-解压-2" class="headerlink" title="② 解压"></a>② 解压</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># tar -zxvf nginx-1.12.1.tar.gz</span><br><span class="line"># cd nginx-1.12.1</span><br></pre></td></tr></table></figure>

<h3 id="③-使用默认配置"><a href="#③-使用默认配置" class="headerlink" title="③ 使用默认配置"></a>③ 使用默认配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># .&#x2F;configure</span><br></pre></td></tr></table></figure>

<h3 id="④-编译、安装"><a href="#④-编译、安装" class="headerlink" title="④ 编译、安装"></a>④ 编译、安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># make</span><br><span class="line"># make install</span><br></pre></td></tr></table></figure>

<h3 id="⑤-启动nginx"><a href="#⑤-启动nginx" class="headerlink" title="⑤ 启动nginx"></a>⑤ 启动nginx</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;</span><br><span class="line"># .&#x2F;nginx </span><br><span class="line"></span><br><span class="line">其它命令</span><br><span class="line"># .&#x2F;nginx -s stop</span><br><span class="line"># .&#x2F;nginx -s quit</span><br><span class="line"># .&#x2F;nginx -s reload</span><br></pre></td></tr></table></figure>

<h3 id="⑥-设置开机启动"><a href="#⑥-设置开机启动" class="headerlink" title="⑥ 设置开机启动"></a>⑥ 设置开机启动</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"># vim &#x2F;etc&#x2F;rc.local</span><br><span class="line"></span><br><span class="line">添加一行：</span><br><span class="line">&#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx</span><br><span class="line"></span><br><span class="line"># 设置执行权限</span><br><span class="line"># chmod 755 &#x2F;etc&#x2F;rc.local</span><br></pre></td></tr></table></figure>

<h3 id="⑦-查看nginx的版本及模块"><a href="#⑦-查看nginx的版本及模块" class="headerlink" title="⑦ 查看nginx的版本及模块"></a>⑦ 查看nginx的版本及模块</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx -V</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-200634@2x.png" alt=""></p>
<h3 id="⑧-防火墙中打开Nginx端口（默认的-80）"><a href="#⑧-防火墙中打开Nginx端口（默认的-80）" class="headerlink" title="⑧ 防火墙中打开Nginx端口（默认的 80）"></a>⑧ 防火墙中打开Nginx端口（默认的 80）</h3><p>添加后就能在本机使用80端口访问了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 添加开放IP：</span><br><span class="line">firewall-cmd --permanent --zone&#x3D;public --add-port&#x3D;80&#x2F;tcp</span><br><span class="line"># 重启： </span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>


<h2 id="3、访问文件"><a href="#3、访问文件" class="headerlink" title="3、访问文件"></a>3、访问文件</h2><p>简单的测试访问文件</p>
<h3 id="①-修改nginx-conf"><a href="#①-修改nginx-conf" class="headerlink" title="① 修改nginx.conf"></a>① 修改nginx.conf</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># vim &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;conf&#x2F;nginx.conf</span><br><span class="line"></span><br><span class="line">添加如下行，将 &#x2F;group1&#x2F;M00 映射到 &#x2F;data&#x2F;fastdfs&#x2F;file&#x2F;data</span><br><span class="line">location &#x2F;group1&#x2F;M00 &#123;</span><br><span class="line">    alias &#x2F;data&#x2F;fastdfs&#x2F;file&#x2F;data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 重启nginx</span><br><span class="line"># &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx -s reload</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180409-201024@2x.png" alt=""></p>
<p>② 在浏览器访问之前上传的图片、成功。</p>
<p><a href="http://118.25.36.41/group1/M00/00/00/Cmlg2FrLU9eAVJ-ZAAAejzOgbzU97.conf" target="_blank" rel="noopener">http://118.25.36.41/group1/M00/00/00/Cmlg2FrLU9eAVJ-ZAAAejzOgbzU97.conf</a></p>
<h1 id="四、FastDFS-配置-Nginx-模块"><a href="#四、FastDFS-配置-Nginx-模块" class="headerlink" title="四、FastDFS 配置 Nginx 模块"></a>四、FastDFS 配置 Nginx 模块</h1><h2 id="1、安装配置Nginx模块"><a href="#1、安装配置Nginx模块" class="headerlink" title="1、安装配置Nginx模块"></a>1、安装配置Nginx模块</h2><h3 id="①-fastdfs-nginx-module-模块说明"><a href="#①-fastdfs-nginx-module-模块说明" class="headerlink" title="① fastdfs-nginx-module 模块说明"></a>① fastdfs-nginx-module 模块说明</h3><p>FastDFS 通过 Tracker 服务器，将文件放在 Storage 服务器存储， 但是同组存储服务器之间需要进行文件复制， 有同步延迟的问题。</p>
<p>　　假设 Tracker 服务器将文件上传到了 192.168.51.128，上传成功后文件 ID已经返回给客户端。</p>
<p>　　此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.51.129，在文件还没有复制完成的情况下，客户端如果用这个文件 ID 在 192.168.51.129 上取文件,就会出现文件无法访问的错误。</p>
<p>　　而 fastdfs-nginx-module 可以重定向文件链接到源服务器取文件，避免客户端由于复制延迟导致的文件无法访问错误。
　　</p>
<h3 id="②-下载-fastdfs-nginx-module、解压"><a href="#②-下载-fastdfs-nginx-module、解压" class="headerlink" title="② 下载 fastdfs-nginx-module、解压"></a>② 下载 fastdfs-nginx-module、解压</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 这里为啥这么长一串呢，因为最新版的master与当前nginx有些版本问题。</span><br><span class="line"># wget https:&#x2F;&#x2F;github.com&#x2F;happyfish100&#x2F;fastdfs-nginx-module&#x2F;archive&#x2F;5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip</span><br><span class="line"></span><br><span class="line"># 解压</span><br><span class="line"># unzip 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip</span><br><span class="line"></span><br><span class="line"># 重命名</span><br><span class="line"># mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1  fastdfs-nginx-module-master</span><br></pre></td></tr></table></figure>

<h3 id="③-配置Nginx"><a href="#③-配置Nginx" class="headerlink" title="③ 配置Nginx"></a>③ 配置Nginx</h3><p>在nginx中添加模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 先停掉nginx服务</span><br><span class="line"># &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;ngix -s stop</span><br><span class="line"></span><br><span class="line">进入解压包目录</span><br><span class="line"># cd &#x2F;softpackages&#x2F;nginx-1.12.1&#x2F;</span><br><span class="line"></span><br><span class="line"># 添加模块</span><br><span class="line"># .&#x2F;configure --add-module&#x3D;..&#x2F;fastdfs-nginx-module-master&#x2F;src</span><br><span class="line"></span><br><span class="line">重新编译、安装</span><br><span class="line"># make &amp;&amp; make install</span><br></pre></td></tr></table></figure>

<h3 id="④-查看Nginx的模块"><a href="#④-查看Nginx的模块" class="headerlink" title="④ 查看Nginx的模块"></a>④ 查看Nginx的模块</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx -V</span><br></pre></td></tr></table></figure>

<p>有下面这个就说明添加模块成功</p>
<p><img src="http://img.winterchen.com/WX20180411-150859@2x.png" alt=""></p>
<h3 id="⑤-复制-fastdfs-nginx-module-源码中的配置文件到-etc-fdfs-目录，-并修改"><a href="#⑤-复制-fastdfs-nginx-module-源码中的配置文件到-etc-fdfs-目录，-并修改" class="headerlink" title="⑤ 复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录， 并修改"></a>⑤ 复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录， 并修改</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;softpackages&#x2F;fastdfs-nginx-module-master&#x2F;src</span><br><span class="line"></span><br><span class="line"># cp mod_fastdfs.conf &#x2F;etc&#x2F;fdfs&#x2F;</span><br></pre></td></tr></table></figure>

<p>修改如下配置，其它默认</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;softpackages&#x2F;fastdfs-nginx-module-master&#x2F;src</span><br><span class="line"></span><br><span class="line"># cp mod_fastdfs.conf &#x2F;etc&#x2F;fdfs&#x2F;</span><br></pre></td></tr></table></figure>

<p>修改如下配置，其它默认</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 连接超时时间</span><br><span class="line">connect_timeout&#x3D;10</span><br><span class="line"></span><br><span class="line"># Tracker Server</span><br><span class="line">tracker_server&#x3D;118.25.36.41:22122</span><br><span class="line"></span><br><span class="line"># StorageServer 默认端口</span><br><span class="line">storage_server_port&#x3D;23000</span><br><span class="line"></span><br><span class="line"># 如果文件ID的uri中包含&#x2F;group**，则要设置为true</span><br><span class="line">url_have_group_name &#x3D; true</span><br><span class="line"></span><br><span class="line"># Storage 配置的store_path0路径，必须和storage.conf中的一致</span><br><span class="line">store_path0&#x3D;&#x2F;data&#x2F;fastdfs&#x2F;file</span><br></pre></td></tr></table></figure>

<h3 id="⑥-复制-FastDFS-的部分配置文件到-etc-fdfs-目录"><a href="#⑥-复制-FastDFS-的部分配置文件到-etc-fdfs-目录" class="headerlink" title="⑥ 复制 FastDFS 的部分配置文件到/etc/fdfs 目录"></a>⑥ 复制 FastDFS 的部分配置文件到/etc/fdfs 目录</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># cd &#x2F;softpackages&#x2F;fastdfs-5.05&#x2F;conf&#x2F;</span><br><span class="line"></span><br><span class="line"># cp anti-steal.jpg http.conf mime.types &#x2F;etc&#x2F;fdfs&#x2F;</span><br></pre></td></tr></table></figure>

<h3 id="⑦-配置nginx，修改nginx-conf"><a href="#⑦-配置nginx，修改nginx-conf" class="headerlink" title="⑦ 配置nginx，修改nginx.conf"></a>⑦ 配置nginx，修改nginx.conf</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># vim &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;conf&#x2F;nginx.conf</span><br></pre></td></tr></table></figure>

<p>修改配置，其它的默认</p>
<p>在80端口下添加fastdfs-nginx模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location ~&#x2F;group([0-9])&#x2F;M00 &#123;</span><br><span class="line">    ngx_fastdfs_module;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="http://img.winterchen.com/WX20180411-150844@2x.png" alt=""></p>
<p><strong>注意：</strong><br>listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相对应。如果改成其它端口，则需要统一，同时在防火墙中打开该端口。</p>
<p>　　location 的配置，如果有多个group则配置location ~/group([0-9])/M00 ，没有则不用配group。
　　</p>
<h3 id="⑧-在-data-fastdfs-file-文件存储目录下创建软连接，将其链接到实际存放数据的目录，这一步可以省略。"><a href="#⑧-在-data-fastdfs-file-文件存储目录下创建软连接，将其链接到实际存放数据的目录，这一步可以省略。" class="headerlink" title="⑧ 在/data/fastdfs/file 文件存储目录下创建软连接，将其链接到实际存放数据的目录，这一步可以省略。"></a>⑧ 在/data/fastdfs/file 文件存储目录下创建软连接，将其链接到实际存放数据的目录，这一步可以省略。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># ln -s &#x2F;data&#x2F;fastdfs&#x2F;file&#x2F;data&#x2F; &#x2F;data&#x2F;fastdfs&#x2F;file&#x2F;data&#x2F;M00</span><br></pre></td></tr></table></figure>

<h3 id="⑨-启动nginx"><a href="#⑨-启动nginx" class="headerlink" title="⑨ 启动nginx"></a>⑨ 启动nginx</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx</span><br></pre></td></tr></table></figure>

<p>打印处如下就算配置成功</p>
<p><img src="http://img.winterchen.com/WX20180411-151138@2x.png" alt=""></p>
<h3 id="⑩-在地址栏访问"><a href="#⑩-在地址栏访问" class="headerlink" title="⑩ 在地址栏访问"></a>⑩ 在地址栏访问</h3><p>能下载文件就算安装成功。注意和第三点中直接使用nginx路由访问不同的是，这里配置 fastdfs-nginx-module 模块，可以重定向文件链接到源服务器取文件。</p>
<p><a href="http://118.25.36.41/group1/M00/00/00/Cmlg2FrLU9eAVJ-ZAAAejzOgbzU97.conf" target="_blank" rel="noopener">http://118.25.36.41/group1/M00/00/00/Cmlg2FrLU9eAVJ-ZAAAejzOgbzU97.conf</a></p>
<p>最终部署结构图(盗的图)：可以按照下面的结构搭建环境。<br><img src="http://img.winterchen.com/WX20180410-092201@2x.png" alt=""></p>
<p>以上</p>
<p>原作者：bojiangzhou<br>原出处：<a href="http://www.cnblogs.com/chiangchou/" target="_blank" rel="noopener">http://www.cnblogs.com/chiangchou/</a>
　　</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='https://raw.githubusercontent.com/WinterChenS/imgrpo/master/mp3/%E6%96%B0%E8%A3%A4%E5%AD%90-%E7%94%9F%E6%B4%BB%E5%9B%A0%E4%BD%A0%E8%80%8C%E7%81%AB%E7%83%AD.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='false'
        data-ae='false'
        data-ci='77f2adc33e0112d086d7'
        data-cs='e926aa3a621556609b2c2b76d89320fa524e23cd'
        data-r='WinterChenS.github.io'
        data-o='WinterChenS'
        data-a='WinterChenS'
        data-d='false'
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>





<!-- Google Analytics -->
<script type="text/javascript">
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'G-30C04D6TMS', 'auto');
  ga('send', 'pageview');
</script>
<!-- End Google Analytics -->


</html>
